//
//  ShoppingCar.m
//  Wujin
//
//  Created by 东 on 16/12/10.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "ShoppingCar.h"

#import "OrderDetail.h"

@interface ShoppingCar ()

@end

@implementation ShoppingCar


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _deleteBtn.layer.cornerRadius = 5;
    _deleteBtn.clipsToBounds = YES;
    _makeOrder.layer.cornerRadius = 5;
    _makeOrder.clipsToBounds = YES;
    _allSelectBtn.layer.cornerRadius = 5;
    _allSelectBtn.clipsToBounds = YES;
    
    self.orderArr = [NSMutableArray array];
    self.markArr   = [NSMutableArray array];
    _dataArray = [NSMutableArray array];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self loadData];
}

#pragma mark - 删除
//删除按钮点击事件
- (IBAction)deleteClick:(UIButton *) button
{
    
    for (OrderGoodsModel *model in self.dataArray)
    {
        if (model.isDelete)
        {
    
            NSString *sql = F(@"delete from db_goods where Gid=%@", model.Gid);
            [dbase begin];
            BOOL success = [dbase exec:sql];
            [dbase commit];
            DLog(@"%d", success);
            if (success)
            {
                [MRHud dismissWithSuccess:@"删除成功"];
            }
            else
            {
                [MRHud dismissWithFailure:@"删除失败"];
            }
        }
    }
    
    //删除
    [self loadData];
}
- (IBAction)allSelect:(id)sender
{
    for (OrderGoodsModel *model in self.dataArray)
    {
        model.isDelete = YES;
    }
    
    [self.tableView reloadData];
}

#pragma mark - 生成订单
- (IBAction)makeOrder:(id)sender
{
    [_orderArr removeAllObjects];
    for (OrderGoodsModel *model in self.dataArray)
    {
        if (model.isDelete)
        {
            [_orderArr addObject:model];
        }
    }
    
    if (_orderArr.count)
    {
        OrderDetail *detail = [UIViewController storyboardWithName:@"Car" storyboardID:@"orderdetail"];
        detail.dataArray = _orderArr;
        [self pushViewController:detail];
    }
    else
    {
        [MRHud show];
        [MRHud dismissWithFailure:@"请选择有效的商品"];
    }
}

#pragma mark tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"goodscell"];
    
    OrderGoodsModel *model = _dataArray[indexPath.row];
    
    cell.model = model;
    
    cell.block = ^(){
    
        [_tableView reloadData];
    };
    
    return cell;
}

#pragma mark - 加载数据
- (void)loadData
{
    [_dataArray removeAllObjects];
    MRRecordset *set = [dbase selectForTable:DB_GOODS order:@"Datetime ASC" where:nil];
    
    while ([set next])
    {
        NSDictionary *dict = [set dictionary];
        OrderGoodsModel *model = [OrderGoodsModel mj_objectWithKeyValues:dict];
        [_dataArray addObject:model];
    };
    
    if (_dataArray.count)
    {
        [self.tableView setNoDataWithTitle:@" "];
    }
    else
    {
        [self.tableView setNoDataWithTitle:@"暂无商品"];
    }

    [self.tableView reloadData];
}

@end


@implementation GoodsCell

- (void)awakeFromNib
{
    _checkBtn.layer.cornerRadius = 3;
    _checkBtn.layer.borderWidth = 2;
    _checkBtn.layer.borderColor = [UIColor blackColor].CGColor;
    _checkBtn.clipsToBounds = YES;
    
    [super awakeFromNib];
}

- (void)setModel:(OrderGoodsModel *)model
{
    _model = model;
    
    [_goodsImg sd_setImageWithURL:[NSURL URLWithString:_model.Url]];
    
    _nameLab.text = _model.Name;
        
    NSArray *array = [_model.Type componentsSeparatedByString:@"+"];
    DLog(@"%@,%@",array[0],array[1]);
    if ([array[0] isEqualToString:@"(null)"] && [array[1] isEqualToString:@"(null)"]) {
        _typeLab.text = @"暂无选择";
    }else if ([array[0] isEqualToString:@"(null)"]&& ![array[1] isEqualToString:@"(null)"]){
        _typeLab.text = array[1];
    }else if(![array[0] isEqualToString:@"(null)"]&& [array[1] isEqualToString:@"(null)"]){
        _typeLab.text = array[0];
    }else{
        _typeLab.text = _model.Type;
    }
    
    _countLab.text = _model.Count;
    _priceLab.text = _model.Price;
    _totalPriceLab.text = _model.TotalPrice;
    
    _checkBtn.selected = _model.isDelete;
    _checkBtn.backgroundColor = _checkBtn.selected ? [UIColor blackColor] : [UIColor whiteColor];
}

- (IBAction)selectDeleteGoods:(UIButton *)btn
{
    _model.isDelete = !_model.isDelete;
    
    if (_block)
    {
        _block();
    }
}

@end
